/**
 * Copyright ©2016-2020 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 */
package profile.hujia.profile.elasticsearch.study.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * class info
 *
 * @author hujia
 * @date 2019-09-03 19:53
 */
@Configuration
public class MyRestClient {

    private final SystemArgument systemArgument;

    public MyRestClient(SystemArgument systemArgument) {
        this.systemArgument = systemArgument;
    }

    @Bean
    public RestClientBuilder restClientBuilder() {
        HttpHost[] hosts = systemArgument.getElasticsearchClusterIP().stream()
                .map(s -> {
                    String[] tmp = s.split(":");
                    return new HttpHost(tmp[0], Integer.parseInt(tmp[1]), "http");
                }).toArray(HttpHost[]::new);
        return RestClient.builder(hosts);
    }

    @Bean("restClient")
    public RestHighLevelClient restClient(RestClientBuilder restClientBuilder) {
        return new RestHighLevelClient(restClientBuilder);
    }
}
